#Run on R Version 3.2.2
#All versions of packages used are listed at the bottom of this file

## ---- estimation_data ----
set.seed(11202015)
#Load packages
library('plyr');library('dplyr'); library('sandwich'); library('lmtest'); library('ggplot2'); library('ggthemes'); library('car'); library('multiwayvcov'); library('xtable'); library('lfe'); library('broom');  library('AER'); library('stargazer'); library('grid'); library('doParallel')

#Load data
load('estimation_data.RData')


## ---- rejection_variation ----

#
#Figure 2
#

rejection_rates <- group_by(contas_data, relator) %>%
  summarise(reject_rates = mean(rejected, na.rm = TRUE) * 100, num_case = n()) %>%
  filter(num_case > 50)

ggplot(rejection_rates, aes(x = reject_rates)) +
  geom_histogram(binwidth=3, size = 2) +
  theme_bw() +
  xlab("Rejection Rate (%)")



## ---- results ----

#
#Core Results
#

hypo1_models <-  list()
hypo1_models$model1 <- felm(rejected ~ 0 | block | (political ~ assigned_political) | cluster,
                            data = na.omit(contas_data[, c("rejected", "political", "assigned_political", "block", "cluster")]))

hypo2_models <- list()
hypo2_models$model1 <- felm(rejected ~ 0 | party_block | (political_nonparty_match ~ assigned_political_nonparty_match) | cluster,
                            data = na.omit(contas_data[contas_data$assigned_political_party_match == 0, c("rejected", 'political_nonparty_match', "party_block", "cluster", "assigned_political_nonparty_match")]))

hypo2_models$model2 <- felm(rejected ~ 0 | party_block | (political_party_match ~ assigned_political_party_match) | cluster,
                            data = na.omit(contas_data[contas_data$assigned_political_nonparty_match == 0, c("rejected", 'political_party_match', "party_block", "cluster", "assigned_political_party_match")]))

hypo2_models$model3 <- felm(rejected ~ 0 | party_block | (party_match ~ assigned_party_match) | cluster,
                            data = na.omit(contas_data[contas_data$assigned_technical == 0, c("rejected", 'party_match', "party_block", "cluster", "assigned_party_match")]))

hypo3_model <- list()
hypo3_model$model1 <- felm(rejected ~ 0 | party_block | (coalition_match ~ assigned_coalition_match) | cluster,
                           data = na.omit(contas_data[contas_data$assigned_technical == 0, c("rejected", "coalition_match", "party_block", "cluster", "assigned_coalition_match")]))

hypo4a_models <- list()
hypo4a_models$model1 <- felm(rejected ~ 0 | block | (appointed_technical ~ assigned_tech_appoint) | cluster,
                             data = na.omit(contas_data[contas_data$assigned_technical == 1, c("rejected", 'appointed_technical', "block", "cluster", "assigned_tech_appoint")]))

hypo4b_models <- list()
hypo4b_models$model1 <- felm(rejected ~ 0 | party_block | (party_match ~ assigned_party_match) | cluster,
                             data = na.omit(contas_data[contas_data$assigned_tech_appoint == 1, c("rejected", "party_match", "assigned_party_match", "party_block", "cluster")]))


#Results described in the text
pol_vs_unappointed <- (felm(rejected ~ 0 | block | (political ~ assigned_political) | cluster,
                            data = na.omit(contas_data[contas_data$assigned_appointed_technical == 0, c("rejected", "block", "assigned_political", "political", "cluster")])))

copart_vs_unappointed <-  felm(rejected ~ 0 | party_block | (political_party_match ~ assigned_political_party_match) | cluster,
                               data = na.omit(contas_data[contas_data$assigned_appointed_technical == 0 & contas_data$assigned_political_nonparty_match == 0, c("rejected", 'political_party_match', "party_block", "cluster", "assigned_political_party_match")]))
pol_vs_appointed <- felm(rejected ~ 0 | block | (political ~ assigned_political) | cluster,
                         data = na.omit(contas_data[contas_data$substitute == 0, c("rejected", "political", "assigned_political", "block", "cluster")]))


## ---- multiple_testing ----

#Code for making multiple testing adjustments
source('multple_testing_functions.R')

#Calculate multiple testing adjusted p-values
full_formulas <- c(rejected ~ assigned_political | block | 0 |cluster,
                   rejected ~ assigned_political_nonparty_match | party_block | 0 |cluster,
                   rejected ~ assigned_party_match | party_block |0 | cluster,
                   rejected ~ assigned_party_match | party_block |0 | cluster,
                   rejected ~ assigned_coalition_match | party_block | 0 | cluster,
                   rejected ~ assigned_tech_appoint | block |0 | cluster,
                   rejected ~ assigned_party_match | party_block | 0 |cluster)

null_formulas <- c(rejected ~  0 | block | 0 | cluster,
                   rejected ~  0 |party_block| 0 |cluster,
                   rejected ~  0 | party_block| 0 |cluster,
                   rejected ~  0 | party_block| 0 |cluster,
                   rejected ~  0 | party_block | 0 | cluster,
                   rejected ~  0 | block| 0 |cluster,
                   rejected ~  0 | party_block| 0 |cluster)

var_names <- c("assigned_political", "assigned_political_nonparty_match", "assigned_party_match", "assigned_party_match", "assigned_coalition_match", "assigned_tech_appoint", "assigned_party_match")


subsets <- list('is.na(state) == FALSE',
                'assigned_political_party_match == 0',
                'assigned_political_nonparty_match == 0',
                'assigned_technical == 0',
                'assigned_technical == 0',
                'assigned_technical == 1',
                'assigned_tech_appoint == 1'
)

# warning: the following bootstrap can take several hours. 
cluster <- makeCluster(6)
registerDoParallel(cluster)
adj_p <- boot_stepdown(null_formulas = null_formulas, full_formulas = full_formulas,
                       data = contas_data,
                       coef_list = var_names,
                       subsets = subsets,
                       parallel = TRUE, nboots = 10000)
stopCluster(cluster)

## ---- plots ----

#Create plot objects with main results

plot1_data <- ldply(list(hypo1_models$model1, hypo2_models$model1, hypo2_models$model2, hypo2_models$model3, hypo3_model$model1),
                    tidy)
plot1_data$model <- factor(x = c('Political vs\nBureaucrat', 'Political (Different Party) vs\nBureaucrat', 'Political (Same Party) vs\nBureaucrat', 'Political (Same Party) vs\nPolitical (Different Party)', 'Political (Same Coalition) vs\nPolitical (Different Coalition)'),
                           levels = c('Political (Same Coalition) vs\nPolitical (Different Coalition)', 'Political (Same Party) vs\nPolitical (Different Party)', 'Political (Same Party) vs\nBureaucrat', 'Political (Different Party) vs\nBureaucrat',  'Political vs\nBureaucrat' )
)
plot1_data$n <- sapply(list(hypo1_models$model1, hypo2_models$model1, hypo2_models$model2, hypo2_models$model3, hypo3_model$model1),
                       function(x) x$N)
plot1_data$adj_p <- adj_p$bs_pvalues_adjusted[1:5]

plot1 <- ggplot(plot1_data, aes(y = model, x = estimate)) +
  geom_errorbarh(aes(xmax = estimate + 1.96 * std.error,
                     xmin = estimate - 1.96 * std.error),
                 height = 0, color = 'darkgray') +
  geom_point(size = 4) +
  scale_x_continuous(limits = c(-.1, .04), breaks = c(0, -.03,-.06,-.09), labels = c("0", "-0.03", "-0.06", "-0.09")) +
  xlab("Effect on Accounts Rejection") +
  ylab("") +
  geom_vline(xintercept = 0, linetype = 3, size = .25) +
  geom_text(aes(label = paste0("adjusted p-value = ", sprintf("%.3f", round(plot1_data$adj_p, 3))), x = .01, y = model), vjust=1.4, hjust = .2, size = 2.5, show_guide  = FALSE) +
  theme_tufte() +
  theme( axis.title.x = element_text(hjust=0.2))


plot2_data <- ldply(list(hypo4a_models$model1, hypo4b_models$model1),
                    tidy)
plot2_data$model <- factor(x = c('Appointed Bureacrats\nUnappointed Bureaucrat', 'Appointed Bureaucrat (Same Party) vs\nAppointed Bureaucrat (Different Party)'),
                           levels = c( 'Appointed Bureaucrat (Same Party) vs\nAppointed Bureaucrat (Different Party)', 'Appointed Bureacrats\nUnappointed Bureaucrat')
)
plot2_data$n <- sapply(list(hypo4a_models$model1, hypo4b_models$model1),
                       function(x) x$N)
plot2_data$adj_p <- NA
plot2_data$adj_p[1:2] <- adj_p$bs_pvalues_adjusted[6:7]

plot2 <- ggplot(plot2_data, aes(y = model, x = estimate)) +
  geom_errorbarh(aes(xmax = estimate + 1.96 * std.error,
                     xmin = estimate - 1.96 * std.error),
                 height = 0, color = 'darkgray') +
  geom_point(size = 4) +
  scale_x_continuous(limits = c(-.1, .06), breaks = c(.03, 0, -.03,-.06,-.09), labels = c("0.03","0", "-0.03", "-0.06", "-0.09")) +
  xlab("Effect on Accounts Rejection") +
  ylab("") +
  geom_vline(xintercept = 0, linetype = 3, size = .25) +
  geom_text(aes(label = paste0("adjusted p-value = ", sprintf("%.3f", round(plot2_data$adj_p[1:2], 3))), x = .025, y = model[1:2]), vjust=.5, hjust = .2, size = 2.5, show_guide  = FALSE) +
  theme_tufte()

## ---- main_results_plot ----

#
#Figure 3
#

plot1

## ---- hetero_table ----

#
#Table 3
#


hetero_models <- list()
hetero_models$diverse <- felm(rejected ~  0 | block | (political|diverse|I(political*diverse) ~ (assigned_political + diverse + I(assigned_political * diverse)))| cluster,
                              data = na.omit(contas_data[, c("rejected", "political", "assigned_political", "block", "cluster", "diverse")]))

hetero_models$large_municipality1 <- felm(rejected ~  0 | block | (political | large_municipality | I(political * large_municipality) ~ (assigned_political + large_municipality + I(assigned_political * large_municipality)))| cluster,
                                          data = na.omit(contas_data[, c("rejected", "political", "assigned_political", "block", "cluster", "large_municipality")]))

hetero_models$large_municipality2 <- felm(rejected ~ 0 | party_block | (party_match | large_municipality | I(party_match * large_municipality) ~ assigned_party_match + large_municipality + I(assigned_party_match * large_municipality)) | cluster,
                                          data = na.omit(contas_data[contas_data$assigned_technical == 0, c("rejected", 'party_match', "party_block", "cluster", "assigned_party_match", "large_municipality")]))

hetero_models$early_period1 <- felm(rejected ~  0 | block | (political | early_period| I(political * early_period) ~ (assigned_political + early_period+ I(assigned_political * early_period)))| cluster,
                                    data = na.omit(contas_data[, c("rejected", "political", "assigned_political", "block", "cluster", "early_period")]))

hetero_models$early_period2 <- felm(rejected ~ 0 | party_block | (party_match | early_period | I(party_match * early_period) ~ assigned_party_match + early_period + I(assigned_party_match * early_period)) | cluster,
                                    data = na.omit(contas_data[contas_data$assigned_technical == 0, c("rejected", 'party_match', "party_block", "cluster", "assigned_party_match", "early_period")]))


stargazer(hetero_models$large_municipality1, hetero_models$early_period1,hetero_models$diverse, hetero_models$large_municipality2, hetero_models$early_period2,
          omit = '^`diverse|^`large|^`early_period',
          order = c("^`political", "^`I\\(political", "^`party_match", "^`I\\(party_match"),
          covariate.labels = c("Political", "Political x Large Muni", "Political x Early Period", "Political x Diverse",   "Political (Same Party)", "Political (Same Party) x Large Muni", "Political (Same Party) x Early Period"),
          type = "latex",
          keep.stat = "n",
          dep.var.labels= "Accounts Rejected",
          float = FALSE,
          font.size = "footnotesize",
          add.lines = list(c("Block Fixed Effects", "X", "X", "X", "", ""),
                           c("Party x Block Fixed Effects", "", "", "", "X", "X")))


## ---- substitute_results_plot ----

#
#Figure 4
#

plot2


## ---- compare_substitutes ----

#
#Figure 5
#

tech_rejection_by_year <- group_by(filter(contas_data, technical == 1), year, state, relator, year_appointment, technical, substitute, appointed_technical) %>%
  summarise(rejection_rate = mean(rejected), n = n())
tech_rejection_by_year$after_appt <- ifelse(tech_rejection_by_year$year > tech_rejection_by_year$year_appointment, 1, 0)
tech_rejection_by_year$ever_substitute <- ifelse(tech_rejection_by_year$relator %in%
                                                   na.omit(unique(tech_rejection_by_year$relator[tech_rejection_by_year$after_appt == 0])), 1, 0)
tech_rejection_by_year$ever_substitute[tech_rejection_by_year$substitute == 1] <- 1
tech_rejection_by_year$after_appt[is.na(tech_rejection_by_year$after_appt)] <- 0

compare_tech <- group_by(filter(tech_rejection_by_year, ever_substitute == 1), state,  appointed_technical, relator) %>%
  summarise(rejection_rate = mean(rejection_rate, na.rm = TRUE), num_cases = sum(n))  %>%
  group_by(appointed_technical) %>%
  summarise(rejection_rate = mean(rejection_rate), n = n(), num_cases = sum(num_cases))

compare_substitutes <- group_by(filter(tech_rejection_by_year, ever_substitute == 1 & state != 'RJ'), state, relator, appointed_technical) %>%
  summarise(rejection_rate = 100 * mean(rejection_rate, na.rm = TRUE), num_cases = sum(n))
compare_substitutes <- filter(compare_substitutes, num_cases > 10)
compare_substitutes$state[compare_substitutes$state == 'BA'] <- 'Bahia'
compare_substitutes$state[compare_substitutes$state == 'MA'] <- 'Maranhão'
compare_substitutes$state[compare_substitutes$state == 'PE'] <- 'Pernambuco'
compare_substitutes$state[compare_substitutes$state == 'MG'] <- 'Minas Gerais'
compare_substitutes$state[compare_substitutes$state == 'RJ'] <- 'Rio de Janeiro'
compare_substitutes$state[compare_substitutes$state == 'RS'] <- 'Rio Grande do Sul'
compare_substitutes$state <- reorder(compare_substitutes$state, compare_substitutes$rejection_rate, mean)

ggplot(compare_substitutes, aes(x = rejection_rate, y = state)) +
  geom_point(size = 4) +
  theme_tufte() +
  xlab("% of Accounts Rejected") +
  ylab('')


#
#
# APPENDIX
#
#

## ---- balance_statistics ----

#
#Appendix Table 1
#


##Function for Calculating Balance Statistics

get_bal_stat <- function(variable){
  mod_pol <- felm(as.formula(paste0(variable, " ~ 0 | block | (political ~ assigned_political) | cluster")),
                  data = na.omit(contas_data[, c(variable, 'assigned_political', 'political', 'block', 'cluster')]))
  pol_bal <- data.frame(outcome = variable, treat = "political", coef = coef(mod_pol), se = mod_pol$se, df = mod_pol$df, pvalue = mod_pol$cpval)
  pol_bal$sd <- sd(contas_data[, variable], na.rm = TRUE)
  
  mod_party_bal <- felm(as.formula(paste0(variable, " ~ 0 | party_block | (party_match ~ assigned_party_match) | cluster")),
                        data = na.omit(contas_data[contas_data$substitute != 1 , c(variable, 'assigned_party_match', 'party_match', 'party_block', 'cluster')]))
  party_match_bal <- data.frame(outcome = variable, treat = "party_match", coef = coef(mod_party_bal), se = mod_party_bal$se, df = mod_party_bal$df, pvalue = mod_party_bal$cpval)
  party_match_bal$sd <- sd(contas_data[, variable], na.rm = TRUE)
  
  rbind(pol_bal, party_match_bal)
}

covariates <- c('lag_rejected', 'lag_political', 'log_electorate', 'gov_pref_same_party',  'income_percap_2000', "pref_votepct", "gov_party_pct", "lag2_rejected", 'lag_party_match', 'pres_pt_votepct_2002')
covariate_names <- c('Lag Accounts Rejected (1 Year)', 'Lag Political Councilor', 'Log Electorate', 'Governor, Mayor Co-Partisans', "GDP Per Capita (2000)", "Mayor Vote %", "Governor Vote Share", "Lag Accounts Rejected (2 Years)", 'Lag Councilor & Mayor, Same Party', 'PT Presidential Vote Pct (2002)')



bal_stats <- ldply(covariates, get_bal_stat)
bal_stats$tstat <- bal_stats$coef / bal_stats$se
bal_stats$std_diff <- bal_stats$coef / bal_stats$sd
bal_stats$Covariate <- rep(covariate_names, each = 2)

pol_bal_table <- bal_stats[bal_stats$treat == "political", c('Covariate', 'coef', 'se', 'pvalue')]
names(pol_bal_table) <- c('Covariate', 'Estimate', 'Std. Error', '$p$-value')
pol_bal_table <- pol_bal_table[order(pol_bal_table$Covariate), ]

partymatch_bal_table <- bal_stats[bal_stats$treat == "party_match", c('Covariate', 'coef', 'se', 'pvalue')]
names(partymatch_bal_table) <- c('Covariate', 'Estimate', 'Std. Error', '$p$-value')
partymatch_bal_table <- partymatch_bal_table[order(partymatch_bal_table$Covariate), ]

bal_table <- cbind(pol_bal_table, partymatch_bal_table[, -1])
names(bal_table)[names(bal_table) == "Covariate"] <- ""
bal_table <- xtable(bal_table)
digits(bal_table)[c(3:4,6:7)] <- 3

add_to_row <- list(pos = list(-1, -1, 0),
                   command = c('\\toprule\n',
                               "& \\multicolumn{3}{c}{\\textbf{Political Councilor}} & \\multicolumn{3}{c}{\\textbf{Councilor \\& Mayor, Same Party}}  \\\\\n",
                               "\\cmidrule(r){2-4} \\cmidrule(r){5-7}\n")
)

#F test
pol_bal_mod <- lm(paste0("assigned_political ~ ", paste0(covariates, collapse = " + "), " + block - 1"), data = contas_data)
ftest_pol <- linearHypothesis(pol_bal_mod, paste0(covariates, " = 0"))
party_match_bal_mod <- lm(paste0("assigned_party_match ~ ", paste0(covariates, collapse = " + "), " + party_block - 1"), data = contas_data)
ftest_partymatch <- linearHypothesis(party_match_bal_mod, paste0(covariates, " = 0"), singular.ok = TRUE)

print(bal_table, include.rownames = FALSE,
      sanitize.colnames.function = function(x){x},
      booktabs = TRUE,
      add.to.row = add_to_row,
      hline.after = 10,
      size = "footnotesize",
      floating = FALSE)


## ---- party_estimate_plots ----

#
#Appendix Figure 1
#


party_models <- list()
party_models$pmdb_model <- felm(rejected ~ 0 | party_block | (party_match ~ assigned_party_match) | cluster,
                                data = na.omit(contas_data[contas_data$assigned_technical == 0 & contas_data$pref_party == "PMDB", c("rejected", 'party_match', "party_block", "cluster", "assigned_party_match")]))
party_models$pfl_model <- felm(rejected ~ 0 | party_block | (party_match ~ assigned_party_match) | cluster,
                               data = na.omit(contas_data[contas_data$assigned_technical == 0 & contas_data$pref_party == "PFL", c("rejected", 'party_match', "party_block", "cluster", "assigned_party_match")]))
party_models$psdb_model <- felm(rejected ~ 0 | party_block | (party_match ~ assigned_party_match) | cluster,
                                data = na.omit(contas_data[contas_data$assigned_technical == 0 & contas_data$pref_party == "PSDB", c("rejected", 'party_match', "party_block", "cluster", "assigned_party_match")]))
party_models$pt_model <- felm(rejected ~ 0 | party_block | (party_match ~ assigned_party_match) | cluster,
                              data = na.omit(contas_data[contas_data$assigned_technical == 0 & contas_data$pref_party == "PT", c("rejected", 'party_match', "party_block", "cluster", "assigned_party_match")]))


party_models_plot_data <- ldply(party_models, tidy)
party_models_plot_data$party <- factor(x = c("PMDB", "PFL", "PSDB", "PT"), levels = c("PT", "PSDB", "PFL", "PMDB"))
ggplot(party_models_plot_data, aes(y = party, x = estimate)) +
  geom_errorbarh(aes(xmax = estimate + 1.96 * std.error,
                     xmin = estimate - 1.96 * std.error),
                 height = 0, color = 'darkgray') +
  geom_point(size = 4) +
  xlab("Effect on Accounts Rejection") +
  ylab("") +
  geom_vline(xintercept = 0, linetype = 3, size = .25) +
  theme_tufte()


## ---- state_estimate_plots ----

#
#Appendix Figure 2
#


state_models_political <- contas_data %>%
  group_by(state) %>%
  do(tidy(felm(rejected ~ 0 | block | (political ~ assigned_political) | cluster,
               data = .)))
state_models_political$state <- reorder(state_models_political$state, state_models_political$estimate)


ggplot(state_models_political, aes(y = state, x = estimate)) +
  geom_errorbarh(aes(xmax = estimate + 1.96 * std.error,
                     xmin = estimate - 1.96 * std.error),
                 height = 0, color = 'darkgray') +
  geom_point(size = 4) +
  xlab("Effect on Accounts Rejection") +
  ylab("") +
  geom_vline(xintercept = 0, linetype = 3, size = .25) +
  theme_tufte() +
  ggtitle("Political vs Bureaucrat")


state_models_party_match <- contas_data %>%
  subset(assigned_technical == 0) %>%
  group_by(state) %>%
  do(tidy(felm(rejected ~ 0 | party_block | (party_match ~ assigned_party_match) | cluster,
               data = .)))
state_models_party_match$state <- reorder(state_models_party_match$state, state_models_party_match$estimate)

ggplot(state_models_party_match, aes(y = state, x = estimate)) +
  geom_errorbarh(aes(xmax = estimate + 1.96 * std.error,
                     xmin = estimate - 1.96 * std.error),
                 height = 0, color = 'darkgray') +
  geom_point(size = 4) +
  xlab("Effect on Accounts Rejection") +
  ylab("") +
  geom_vline(xintercept = 0, linetype = 3, size = .25) +
  theme_tufte() +
  ggtitle('Political (Same Party) vs\nPolitical (Different Party)')

## ---- itt_models ----

#
#Appendix Figure 3
#


itt_models <- list()
itt_models$model1 <- felm(rejected ~ assigned_political | block | 0 | cluster,
                          data = na.omit(contas_data[, c("rejected", "political", "assigned_political", "block", "cluster")]))

itt_models$model2 <- felm(rejected ~ assigned_political_nonparty_match | party_block | 0 | cluster,
                          data = na.omit(contas_data[contas_data$assigned_political_party_match == 0, c("rejected", 'political_nonparty_match', "party_block", "cluster", "assigned_political_nonparty_match")]))

itt_models$model3 <- felm(rejected ~ assigned_political_party_match | party_block | 0  | cluster,
                          data = na.omit(contas_data[contas_data$assigned_political_nonparty_match == 0, c("rejected", 'political_party_match', "party_block", "cluster", "assigned_political_party_match")]))

itt_models$model4 <- felm(rejected ~ assigned_party_match | party_block | 0 | cluster,
                          data = na.omit(contas_data[contas_data$assigned_technical == 0, c("rejected", 'party_match', "party_block", "cluster", "assigned_party_match")]))

itt_models$model5 <- felm(rejected ~ coalition_match | party_block | 0 | cluster,
                          data = na.omit(contas_data[contas_data$assigned_technical == 0, c("rejected", "coalition_match", "party_block", "cluster", "assigned_coalition_match")]))

itt_models$model6 <- felm(rejected ~ assigned_tech_appoint | block | 0 | cluster,
                          data = na.omit(contas_data[contas_data$assigned_technical == 1, c("rejected", 'appointed_technical', "block", "cluster", "assigned_tech_appoint")]))

itt_models$model7 <- felm(rejected ~ assigned_party_match | party_block | 0 | cluster,
                          data = na.omit(contas_data[contas_data$assigned_tech_appoint == 1, c("rejected", "party_match", "assigned_party_match", "party_block", "cluster")]))

itt_results <- ldply(itt_models, tidy)
itt_results$term <- factor(x = c('Political vs\nBureaucrat', 'Political (Different Party) vs\nBureaucrat', 'Political (Same Party) vs\nBureaucrat', 'Political (Same Party) vs\nPolitical (Different Party)', 'Political (Same Coalition) vs\nPolitical (Different Coalition)', 'Appointed Bureacrats\nUnappointed Bureaucrat', 'Appointed Bureaucrat (Same Party) vs\nAppointed Bureaucrat (Different Party)'),
                           levels = c( 'Appointed Bureaucrat (Same Party) vs\nAppointed Bureaucrat (Different Party)', 'Appointed Bureacrats\nUnappointed Bureaucrat', 'Political (Same Coalition) vs\nPolitical (Different Coalition)', 'Political (Same Party) vs\nPolitical (Different Party)', 'Political (Same Party) vs\nBureaucrat', 'Political (Different Party) vs\nBureaucrat',  'Political vs\nBureaucrat' )
)

itt_plot <- ggplot(itt_results, aes(y = term, x = estimate)) +
  geom_errorbarh(aes(xmax = estimate + 1.96 * std.error,
                     xmin = estimate - 1.96 * std.error),
                 height = 0, color = 'darkgray') +
  geom_point(size = 4) +
  xlab("Effect on Accounts Rejection") +
  ylab("") +
  geom_vline(xintercept = 0, linetype = 3, size = .25) +
  theme_tufte()

itt_plot

## ---- models_decision_time ----

#
#Appendix Figure 4
#


time_models <-  list()
time_models$model1 <- felm(decision_long ~ 0 | block | (political ~ assigned_political) | cluster,
                           data = na.omit(contas_data[, c("decision_long", "political", "assigned_political", "block", "cluster")]))

time_models$model2 <- felm(decision_long ~ 0 | party_block | (political_nonparty_match ~ assigned_political_nonparty_match) | cluster,
                           data = na.omit(contas_data[contas_data$assigned_political_party_match == 0, c("decision_long", 'political_nonparty_match', "party_block", "cluster", "assigned_political_nonparty_match")]))

time_models$model3 <- felm(decision_long ~ 0 | party_block | (political_party_match ~ assigned_political_party_match) | cluster,
                           data = na.omit(contas_data[contas_data$assigned_political_nonparty_match == 0, c("decision_long", 'political_party_match', "party_block", "cluster", "assigned_political_party_match")]))

time_models$model4 <- felm(decision_long ~ 0 | party_block | (party_match ~ assigned_party_match) | cluster,
                           data = na.omit(contas_data[contas_data$assigned_technical == 0, c("decision_long", 'party_match', "party_block", "cluster", "assigned_party_match")]))

time_models$model5 <- felm(decision_long ~ 0 | party_block | (coalition_match ~ assigned_coalition_match) | cluster,
                           data = na.omit(contas_data[contas_data$assigned_technical == 0, c("decision_long", "coalition_match", "party_block", "cluster", "assigned_coalition_match")]))

time_models$model6 <- felm(decision_long ~ 0 | block | (appointed_technical ~ assigned_tech_appoint) | cluster,
                           data = na.omit(contas_data[contas_data$assigned_technical == 1, c("decision_long", 'appointed_technical', "block", "cluster", "assigned_tech_appoint")]))

time_models$model7 <- felm(decision_long ~ 0 | party_block | (party_match ~ assigned_party_match) | cluster,
                           data = na.omit(contas_data[contas_data$assigned_tech_appoint == 1, c("decision_long", "party_match", "assigned_party_match", "party_block", "cluster")]))

time_results <- ldply(time_models, tidy)
time_results$term <- factor(x = c('Political vs\nBureaucrat', 'Political (Different Party) vs\nBureaucrat', 'Political (Same Party) vs\nBureaucrat', 'Political (Same Party) vs\nPolitical (Different Party)', 'Political (Same Coalition) vs\nPolitical (Different Coalition)', 'Appointed Bureacrats\nUnappointed Bureaucrat', 'Appointed Bureaucrat (Same Party) vs\nAppointed Bureaucrat (Different Party)'),
                            levels = c( 'Appointed Bureaucrat (Same Party) vs\nAppointed Bureaucrat (Different Party)', 'Appointed Bureacrats\nUnappointed Bureaucrat', 'Political (Same Coalition) vs\nPolitical (Different Coalition)', 'Political (Same Party) vs\nPolitical (Different Party)', 'Political (Same Party) vs\nBureaucrat', 'Political (Different Party) vs\nBureaucrat',  'Political vs\nBureaucrat' )
)

time_plot <- ggplot(time_results, aes(y = term, x = estimate)) +
  geom_errorbarh(aes(xmax = estimate + 1.96 * std.error,
                     xmin = estimate - 1.96 * std.error),
                 height = 0, color = 'darkgray') +
  geom_point(size = 4) +
  xlab("Effect on Lengthy Decision Time") +
  ylab("") +
  geom_vline(xintercept = 0, linetype = 3, size = .25) +
  theme_tufte()
time_plot

## ---- appendix_covar_adj ----

#
#Appendix Figure 5
#


covars <- c('pref_party', 'gov_pref_same_party', 'lag_rejected', 'lag_technical', 'lag_substitute', 'lag_party_match')

covars_hypo1 <- c('gov_pref_same_party', 'cum_rejected', 'lag_technical', 'lag_rejected', 'pref_party')
covars_hypo2 <-  c('gov_pref_same_party', 'cum_rejected', 'lag_rejected')
covars_hypo3_1 <-  c('gov_pref_same_party', 'cum_rejected', 'lag_substitute', 'lag_rejected')
covars_hypo3_2 <-  c('gov_pref_same_party', 'cum_rejected', 'lag_party_match', 'lag_rejected')


appendix_covar_models <- list()
appendix_covar_models$model1 <- felm(as.formula(paste0("rejected ~ ", paste0(covars, collapse = "+"), "| block | (political ~ assigned_political) | cluster")),
                                     data = na.omit(contas_data[ ,c('rejected', 'political', 'assigned_political', 'block', 'cluster', covars)]))
appendix_covar_models$model2 <- felm(as.formula(paste0('rejected ~ ', paste0(covars, collapse = ' + '), '| party_block | (political_nonparty_match ~ assigned_political_nonparty_match) | cluster')),
                                     data = na.omit(contas_data[contas_data$political_party_match == 0, c('rejected', 'political_nonparty_match', 'assigned_political_nonparty_match', 'party_block', 'cluster', covars)]))
appendix_covar_models$model3 <- felm(as.formula(paste0('rejected ~ ', paste0(covars, collapse = ' + '), '| party_block | (political_party_match ~ assigned_political_party_match) | cluster')),
                                     data = na.omit(contas_data[contas_data$political_nonparty_match == 0, c('rejected', 'political_party_match', 'assigned_political_party_match', 'party_block', 'cluster', covars)]))
appendix_covar_models$model4 <- felm(as.formula(paste0('rejected ~ ', paste0(covars, collapse = ' + '), '| party_block | (party_match ~ assigned_party_match) | cluster')),
                                     data = na.omit(contas_data[contas_data$technical == 0, c('rejected', 'party_match', 'assigned_party_match', 'party_block', 'cluster', covars)]))
appendix_covar_models$model5 <- felm(rejected ~ 0 | party_block | (coalition_match ~ assigned_coalition_match) | cluster,
                                     data = na.omit(contas_data[contas_data$assigned_technical == 0, c("rejected", "coalition_match", "party_block", "cluster", "assigned_coalition_match")]))
appendix_covar_models$model6 <-  felm(as.formula(paste0('rejected ~ ', paste0(covars, collapse = ' + '), '| block | (appointed_technical ~ assigned_appointed_technical) | cluster')),
                                      data = na.omit(contas_data[contas_data$technical == 1, c('rejected', 'appointed_technical', 'assigned_appointed_technical', 'block', 'cluster', covars)]))
appendix_covar_models$model7 <- felm(as.formula(paste0('rejected ~ ', paste0(covars, collapse = ' + '), '| party_block | (party_match ~ assigned_party_match) | cluster')),
                                     data = na.omit(contas_data[contas_data$assigned_tech_appoint == 1, c("rejected", "party_match", "assigned_party_match", "party_block", "cluster", covars)]))


covar_results <- ldply(appendix_covar_models, tidy)
covar_results <- covar_results[grep("`", covar_results$term), ]

covar_results$term <- factor(x = c('Political vs\nBureaucrat', 'Political (Different Party) vs\nBureaucrat', 'Political (Same Party) vs\nBureaucrat', 'Political (Same Party) vs\nPolitical (Different Party)', 'Political (Same Coalition) vs\nPolitical (Different Coalition)', 'Appointed Bureacrats\nUnappointed Bureaucrat', 'Appointed Bureaucrat (Same Party) vs\nAppointed Bureaucrat (Different Party)'),
                             levels = c( 'Appointed Bureaucrat (Same Party) vs\nAppointed Bureaucrat (Different Party)', 'Appointed Bureacrats\nUnappointed Bureaucrat', 'Political (Same Coalition) vs\nPolitical (Different Coalition)', 'Political (Same Party) vs\nPolitical (Different Party)', 'Political (Same Party) vs\nBureaucrat', 'Political (Different Party) vs\nBureaucrat',  'Political vs\nBureaucrat' )
)

covar_plot <- ggplot(covar_results, aes(y = term, x = estimate)) +
  geom_errorbarh(aes(xmax = estimate + 1.96 * std.error,
                     xmin = estimate - 1.96 * std.error),
                 height = 0, color = 'darkgray') +
  geom_point(size = 4) +
  xlab("Effect on Accounts Rejection") +
  ylab("") +
  geom_vline(xintercept = 0, linetype = 3, size = .25) +
  theme_tufte()
covar_plot


## ---- models_alternative_clustering ----

#
#Appendix Figure 6
#

cluster_models <-  list()
cluster_models$model1 <- felm(rejected ~ 0 | block | (political ~ assigned_political) | ipea.code,
                              data = na.omit(contas_data[, c("rejected", "political", "assigned_political", "block", "ipea.code")]))

cluster_models$model2 <- felm(rejected ~ 0 | party_block | (political_nonparty_match ~ assigned_political_nonparty_match) | ipea.code,
                              data = na.omit(contas_data[contas_data$assigned_political_party_match == 0, c("rejected", 'political_nonparty_match', "party_block", "ipea.code", "assigned_political_nonparty_match")]))

cluster_models$model3 <- felm(rejected ~ 0 | party_block | (political_party_match ~ assigned_political_party_match) | ipea.code,
                              data = na.omit(contas_data[contas_data$assigned_political_nonparty_match == 0, c("rejected", 'political_party_match', "party_block", "ipea.code", "assigned_political_party_match")]))

cluster_models$model4 <- felm(rejected ~ 0 | party_block | (party_match ~ assigned_party_match) | ipea.code,
                              data = na.omit(contas_data[contas_data$assigned_technical == 0, c("rejected", 'party_match', "party_block", "ipea.code", "assigned_party_match")]))

cluster_models$model5 <- felm(rejected ~ 0 | party_block | (coalition_match ~ assigned_coalition_match) | ipea.code,
                              data = na.omit(contas_data[contas_data$assigned_technical == 0, c("rejected", "coalition_match", "party_block", "ipea.code", "assigned_coalition_match")]))

cluster_models$model6 <- felm(rejected ~ 0 | block | (appointed_technical ~ assigned_tech_appoint) | ipea.code,
                              data = na.omit(contas_data[contas_data$assigned_technical == 1, c("rejected", 'appointed_technical', "block", "ipea.code", "assigned_tech_appoint")]))

cluster_models$model7 <- felm(rejected ~ 0 | party_block | (party_match ~ assigned_party_match) | ipea.code,
                              data = na.omit(contas_data[contas_data$assigned_tech_appoint == 1, c("rejected", "party_match", "assigned_party_match", "party_block", "ipea.code")]))

cluster_results <- ldply(cluster_models, tidy)
cluster_results$term <- factor(x = c('Political vs\nBureaucrat', 'Political (Different Party) vs\nBureaucrat', 'Political (Same Party) vs\nBureaucrat', 'Political (Same Party) vs\nPolitical (Different Party)', 'Political (Same Coalition) vs\nPolitical (Different Coalition)', 'Appointed Bureacrats\nUnappointed Bureaucrat', 'Appointed Bureaucrat (Same Party) vs\nAppointed Bureaucrat (Different Party)'),
                               levels = c( 'Appointed Bureaucrat (Same Party) vs\nAppointed Bureaucrat (Different Party)', 'Appointed Bureacrats\nUnappointed Bureaucrat', 'Political (Same Coalition) vs\nPolitical (Different Coalition)', 'Political (Same Party) vs\nPolitical (Different Party)', 'Political (Same Party) vs\nBureaucrat', 'Political (Different Party) vs\nBureaucrat',  'Political vs\nBureaucrat' )
)

cluster_plot <- ggplot(cluster_results, aes(y = term, x = estimate)) +
  geom_errorbarh(aes(xmax = estimate + 1.96 * std.error,
                     xmin = estimate - 1.96 * std.error),
                 height = 0, color = 'darkgray') +
  geom_point(size = 4) +
  xlab("Effect on Accounts Rejection") +
  ylab("") +
  geom_vline(xintercept = 0, linetype = 3, size = .25) +
  theme_tufte()
cluster_plot




## ---- inverse_prop_weighting ----

#
#Appendix Figure 7
#

ipw_mod <- function(treat, iv, block, data){
  ipw_data <- data.frame(data)
  ipw_data$treat <- ipw_data[, treat]
  ipw_data$iv <- ipw_data[, iv]
  ipw_data$block <- ipw_data[, block]
  ipw_data <- group_by(ipw_data, block) %>%
    mutate(ipw = ifelse(iv == 1, 1 / mean(iv),
                        1 / (1 - mean(iv))))
  mod <- felm(rejected ~ 0 | block | (treat ~ iv) | cluster, data = ipw_data, weights = ipw_data$ipw)
  return(mod)
}




ipw_models <- list()

ipw_models$model1 <- ipw_mod(treat = "political", iv = "assigned_political", block = "block",
                             data = na.omit(contas_data[ ,c('rejected', 'political', 'assigned_political', 'block', 'cluster')]))
ipw_models$model2 <- ipw_mod(treat = "political_nonparty_match", iv = 'assigned_political_nonparty_match', block = "party_block",
                             data = na.omit(contas_data[contas_data$political_party_match == 0, c('rejected', 'political_nonparty_match', 'assigned_political_nonparty_match', 'party_block', 'cluster')]))
ipw_models$model3 <- ipw_mod(treat = "political_party_match", iv = 'assigned_political_party_match', block = "party_block",
                             data = na.omit(contas_data[contas_data$political_nonparty_match == 0, c('rejected', 'political_party_match', 'assigned_political_party_match', 'party_block', 'cluster')]))
ipw_models$model4 <- ipw_mod(treat = "party_match", iv = 'assigned_party_match', block = "party_block",
                             data = na.omit(contas_data[contas_data$technical == 0, c('rejected', 'party_match', 'assigned_party_match', 'party_block', 'cluster')]))
ipw_models$model5 <- ipw_mod(treat = "coalition_match", iv = "assigned_coalition_match", block = "party_block",
                             data = na.omit(contas_data[contas_data$assigned_technical == 0, c("rejected", "coalition_match", "party_block", "cluster", "assigned_coalition_match")]))
ipw_models$model6 <- ipw_mod(treat = "appointed_technical", iv = "assigned_tech_appoint", block = "block",
                             data =  na.omit(contas_data[contas_data$assigned_technical == 1, c("rejected", 'appointed_technical', "block", "cluster", "assigned_tech_appoint")]))
ipw_models$model7 <- ipw_mod(treat = "party_match", iv = "assigned_party_match", block = "party_block",
                             data = na.omit(contas_data[contas_data$assigned_tech_appoint == 1, c("rejected", "party_match", "assigned_party_match", "party_block", "cluster")]))

ipw_results <- ldply(ipw_models, tidy)
ipw_results$term <- factor(x = c('Political vs\nBureaucrat', 'Political (Different Party) vs\nBureaucrat', 'Political (Same Party) vs\nBureaucrat', 'Political (Same Party) vs\nPolitical (Different Party)', 'Political (Same Coalition) vs\nPolitical (Different Coalition)', 'Appointed Bureacrats\nUnappointed Bureaucrat', 'Appointed Bureaucrat (Same Party) vs\nAppointed Bureaucrat (Different Party)'),
                           levels = c( 'Appointed Bureaucrat (Same Party) vs\nAppointed Bureaucrat (Different Party)', 'Appointed Bureacrats\nUnappointed Bureaucrat', 'Political (Same Coalition) vs\nPolitical (Different Coalition)', 'Political (Same Party) vs\nPolitical (Different Party)', 'Political (Same Party) vs\nBureaucrat', 'Political (Different Party) vs\nBureaucrat',  'Political vs\nBureaucrat' )
)

ipw_plot <- ggplot(ipw_results, aes(y = term, x = estimate)) +
  geom_errorbarh(aes(xmax = estimate + 1.96 * std.error,
                     xmin = estimate - 1.96 * std.error),
                 height = 0, color = 'darkgray') +
  geom_point(size = 4) +
  xlab("Effect on Accounts Rejection") +
  ylab("") +
  geom_vline(xintercept = 0, linetype = 3, size = .25) +
  theme_tufte()
ipw_plot


## ---- jacknife_robustness ----


#
#Appendix Figure 8
#


relatores <- unique(na.omit(contas_data$relator))

coefs <- c('Political vs\nBureaucrat', 'Political (Different Party) vs\nBureaucrat', 'Political (Same Party) vs\nBureaucrat', 'Political (Same Party) vs\nPolitical (Different Party)', 'Political (Same Coalition) vs\nPolitical (Different Coalition)', 'Appointed Bureacrats\nUnappointed Bureaucrat', 'Appointed Bureaucrat (Same Party) vs\nAppointed Bureaucrat (Different Party)')
jacknife_coefs <- data.frame(relatores = rep(relatores, 7), coef = rep( coefs, length(relatores)), stringsAsFactors = FALSE)
jacknife_coefs$est <- NA
jacknife_coefs$coef <- factor(x = c('Political vs\nBureaucrat', 'Political (Different Party) vs\nBureaucrat', 'Political (Same Party) vs\nBureaucrat', 'Political (Same Party) vs\nPolitical (Different Party)', 'Political (Same Coalition) vs\nPolitical (Different Coalition)', 'Appointed Bureacrats\nUnappointed Bureaucrat', 'Appointed Bureaucrat (Same Party) vs\nAppointed Bureaucrat (Different Party)'),
                              levels = c( 'Appointed Bureaucrat (Same Party) vs\nAppointed Bureaucrat (Different Party)', 'Appointed Bureacrats\nUnappointed Bureaucrat', 'Political (Same Coalition) vs\nPolitical (Different Coalition)', 'Political (Same Party) vs\nPolitical (Different Party)', 'Political (Same Party) vs\nBureaucrat', 'Political (Different Party) vs\nBureaucrat',  'Political vs\nBureaucrat' )
)

for(i in 1:length(relatores)){
  jacknife_coefs$est[jacknife_coefs$relatores == relatores[i] & jacknife_coefs$coef == coefs[1]] <-  (felm(rejected ~ 0 | block | (political ~ assigned_political) | cluster,
                                                                                                           data = na.omit(contas_data[contas_data$assigned_relator != relatores[i], c("rejected", "political", "assigned_political", "block", "cluster")])))$coef

  jacknife_coefs$est[jacknife_coefs$relatores == relatores[i] & jacknife_coefs$coef == coefs[2]] <-  felm(rejected ~ 0 | party_block | (political_nonparty_match ~ assigned_political_nonparty_match) | cluster,
                                                                                                          data = na.omit(contas_data[contas_data$assigned_political_party_match == 0 & contas_data$assigned_relator != relatores[i], c("rejected", 'political_nonparty_match', "party_block", "cluster", "assigned_political_nonparty_match")]))$coef

  jacknife_coefs$est[jacknife_coefs$relatores == relatores[i] & jacknife_coefs$coef == coefs[3]] <-  felm(rejected ~ 0 | party_block | (political_party_match ~ assigned_political_party_match) | cluster,
                                                                                                          data = na.omit(contas_data[contas_data$assigned_political_nonparty_match == 0 & contas_data$assigned_relator != relatores[i], c("rejected", 'political_party_match', "party_block", "cluster", "assigned_political_party_match")]))$coef

  jacknife_coefs$est[jacknife_coefs$relatores == relatores[i] & jacknife_coefs$coef == coefs[4]] <-  felm(rejected ~ 0 | party_block | (party_match ~ assigned_party_match) | cluster,
                                                                                                          data = na.omit(contas_data[contas_data$assigned_technical == 0 & contas_data$assigned_relator != relatores[i], c("rejected", 'party_match', "party_block", "cluster", "assigned_party_match")]))$coef

  jacknife_coefs$est[jacknife_coefs$relatores == relatores[i] & jacknife_coefs$coef == coefs[5]] <-  felm(rejected ~ 0 | party_block | (coalition_match ~ assigned_coalition_match) | cluster,
                                                                                                          data = na.omit(contas_data[contas_data$assigned_technical == 0 & contas_data$assigned_relator != relatores[i], c("rejected", "coalition_match", "party_block", "cluster", "assigned_coalition_match")]))$coef

  jacknife_coefs$est[jacknife_coefs$relatores == relatores[i] & jacknife_coefs$coef == coefs[6]] <-  felm(rejected ~ 0 | block | (appointed_technical ~ assigned_tech_appoint) | cluster,
                                                                                                          data = na.omit(contas_data[contas_data$assigned_technical == 1 & contas_data$assigned_relator != relatores[i], c("rejected", 'appointed_technical', "block", "cluster", "assigned_tech_appoint")]))$coef

  jacknife_coefs$est[jacknife_coefs$relatores == relatores[i] & jacknife_coefs$coef == coefs[7]] <-  felm(rejected ~ 0 | party_block | (party_match ~ assigned_party_match) | cluster,
                                                                                                          data = na.omit(contas_data[contas_data$assigned_tech_appoint == 1 & contas_data$assigned_relator != relatores[i], c("rejected", "party_match", "assigned_party_match", "party_block", "cluster")]))$coef
}

jacknife_coefs$coef <- factor(x = c('Political vs\nBureaucrat', 'Political (Different Party) vs\nBureaucrat', 'Political (Same Party) vs\nBureaucrat', 'Political (Same Party) vs\nPolitical (Different Party)', 'Political (Same Coalition) vs\nPolitical (Different Coalition)', 'Appointed Bureacrats\nUnappointed Bureaucrat', 'Appointed Bureaucrat (Same Party) vs\nAppointed Bureaucrat (Different Party)'),
                              levels = c('Political vs\nBureaucrat', 'Political (Different Party) vs\nBureaucrat', 'Political (Same Party) vs\nBureaucrat', 'Political (Same Party) vs\nPolitical (Different Party)', 'Political (Same Coalition) vs\nPolitical (Different Coalition)', 'Appointed Bureacrats\nUnappointed Bureaucrat', 'Appointed Bureaucrat (Same Party) vs\nAppointed Bureaucrat (Different Party)'))

ggplot(jacknife_coefs, aes(x = est)) +
 geom_histogram(binwidth = .001) +
  facet_wrap(~coef, ncol = 1) +
  theme_tufte()


## ---- heterogeneity_pap ----

#
#Appendix Table 2
#


mod_gov <- felm(rejected ~ 0 | party_block | (party_match ~ assigned_party_match) | cluster,
                data = na.omit(contas_data[contas_data$assigned_technical == 0 & contas_data$assigned_gov_appoint == 1, c("rejected", 'party_match', "party_block", "cluster", "assigned_party_match")]))

mod_leg <- felm(rejected ~ 0 | party_block | (party_match ~ assigned_party_match) | cluster,
                data = na.omit(contas_data[contas_data$assigned_technical == 0 & contas_data$assigned_leg_appoint == 1, c("rejected", 'party_match', "party_block", "cluster", "assigned_party_match")]))


contas_data$tenure_length <- as.numeric(contas_data$year) - contas_data$year_appointment
contas_data$tenure_length[contas_data$assigned_substitute == 1] <- NA
contas_data$tenure_length[contas_data$tenure_length <0 ] <- NA

contas_data$tenure <- ifelse(contas_data$tenure_length > median(contas_data$tenure_length, na.rm = TRUE), 1, 0)
contas_data$gov_party_in_power <- ifelse(contas_data$gov_party == contas_data$assigned_party_governor, 1, 0 )


tenure_mod <- (felm(rejected ~  0 | block | (political|tenure|I(political*tenure) ~ (assigned_political + tenure + I(assigned_political * tenure)))| cluster,
       data = na.omit(contas_data[, c("rejected", "political", "assigned_political", "block", "cluster", "tenure")])))
mod_gov_in_power <- felm(rejected ~ 0 | party_block | (party_match|gov_party_in_power|I(party_match * gov_party_in_power) ~ (assigned_party_match + gov_party_in_power + I(assigned_party_match * gov_party_in_power))) | cluster,
                         data = na.omit(contas_data[contas_data$assigned_technical == 0 & contas_data$assigned_gov_appoint == 1, c("rejected", 'party_match', "party_block", "cluster", "assigned_party_match", "gov_party_in_power")]))


stargazer(mod_gov, mod_leg, tenure_mod,  mod_gov_in_power, type = "latex",
          keep.stat = "n",
          dep.var.labels= "Accounts Rejected",
          float = FALSE,
          column.labels = c("Governor Appt.", "Legislature Appt.", "Tenure Length", "Party in Power"),
          omit = "`tenure|`gov_party",
          covariate.labels = c("Political (Same Party)", "Political", "Political x Tenure", "Political (Same Party) x Party in Power"),
          font.size = "footnotesize",
          add.lines = list(c("Block Fixed Effects", "", "", "X", ""),
                           c("Party x Block Fixed Effects", "X", "X", "", "X"))
  )

# > sessionInfo()
# R version 3.2.2 (2015-08-14)
# Platform: x86_64-apple-darwin14.5.0 (64-bit)
# Running under: OS X 10.11.2 (El Capitan)
# 
# locale:
#   [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
# 
# attached base packages:
#   [1] parallel  grid      stats     graphics  grDevices utils     datasets  methods   base     
# 
# other attached packages:
#   [1] doParallel_1.0.10  iterators_1.0.8    foreach_1.4.3      stargazer_5.2      AER_1.2-4          survival_2.38-3    broom_0.4.0       
# [8] lfe_2.4-1788       Matrix_1.2-3       xtable_1.8-0       multiwayvcov_1.2.2 car_2.1-0          ggthemes_2.2.1     ggplot2_1.0.1     
# [15] lmtest_0.9-34      zoo_1.7-12         sandwich_2.3-4     dplyr_0.4.3        plyr_1.8.3        
# 
# loaded via a namespace (and not attached):
#   [1] Rcpp_0.12.2        compiler_3.2.2     nloptr_1.0.4       tools_3.2.2        boot_1.3-17        digest_0.6.8       lme4_1.1-10       
# [8] gtable_0.1.2       nlme_3.1-122       lattice_0.20-33    mgcv_1.8-9         psych_1.5.8        DBI_0.3.1          SparseM_1.7       
# [15] proto_0.3-10       stringr_1.0.0      MatrixModels_0.4-1 nnet_7.3-11        R6_2.1.1           minqa_1.2.4        Formula_1.2-1     
# [22] tidyr_0.3.1        reshape2_1.4.1     magrittr_1.5       codetools_0.2-14   scales_0.3.0       MASS_7.3-45        splines_3.2.2     
# [29] mnormt_1.5-3       assertthat_0.1     pbkrtest_0.4-2     colorspace_1.2-6   quantreg_5.19      stringi_1.0-1      munsell_0.4.2  

